﻿<html>  
  <head>  
        <meta charset="utf-8">  
        <title>11.3.2.2 平铺在经纬线网格上的中国地图</title>  
  </head> 
  <style>
	.province {
		stroke: black;
		stroke-width: 1px;
	}
  
	.southsea {
		stroke: black;
		stroke-width: 1px;
		fill: red;
	}
	
	.graticule {
		fill: none;
		stroke: #333;
		stroke-width: 1px;
	}
	
  </style>
<body>
<script src="../../../d3/d3.min.js"></script>
<script src="../../../d3/topojson.js"></script>
<script>

var width = 700,
	height = 700;
	
var svg = d3.select("body").append("svg")
	.attr("width", width)
	.attr("height", height);


var projection = d3.geo.mercator()
						.center([107, 31])
						.scale(600)
    					.translate([width/2, height/2]);
	
var path = d3.geo.path()
				.projection(projection);
	
var color = d3.scale.category20();
	
	
d3.json("china.topojson", function(error, toporoot) {
	if (error) 
		return console.error(error);
	
	//输出china.topojson的对象
	console.log(toporoot);
	
	//将TopoJSON对象转换成GeoJSON，保存在georoot中
	var georoot = topojson.feature(toporoot,toporoot.objects.china);
	
	//输出GeoJSON对象
	console.log(georoot);
	
	var eps = 1e-4;
	
	var graticule = d3.geo.graticule()
						.extent([[71, 16],[137, 54]])
						.step([5,5]);
						
	var grid = graticule();
	console.log(grid);

	svg.append("path")
		.datum(grid)
		.attr("class","graticule")
		.attr("d",path);

	var groups = svg.append("g");
		
	groups.selectAll("path")
			.data( georoot.features )
			.enter()
			.append("path")
			.attr("class","province")
			.style("fill",function(d,i){ return color(i); })
			.attr("d", path );
	
});
	
	
d3.xml("southchinasea.svg", function(error, xmlDocument) {
	svg.html(function(d){
		return d3.select(this).html() + xmlDocument.getElementsByTagName("g")[0].outerHTML;
	});
	
	var gSouthSea = d3.select("#southsea");
	
	gSouthSea.attr("transform","translate(540,410)scale(0.5)")
		.attr("class","southsea");

});


</script>
		
    </body>  
</html>  